package com.tjbank.cssys.web.util;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class FileDownloadUtil {

    private static final Logger logger = LoggerFactory.getLogger(FileDownloadUtil.class);
    //地址
    @Value("${endpoint}")
    public   String endpoint /*="http://oss-cn-tianjin-tjyn-d01-a.ops.ypt.bankoftianjin.com"*/;
    @Value("${accessKeyId}")
    public  String accessKeyId/*="G7rR7LpblClpXa3X"*/;
    @Value("${accessKeySecret}")
    public  String accessKeySecret/*="EgOrr6IkHNr7zpXk6HwlHtTqVzeaCv"*/;
    //库名
    @Value("${bucketName}")
    public  String bucketName/*="intelligentoutacall001"*/;


    /**
     * 上传
     *
     * @param key
     * @param file
     */
    public void uploadOSS(String key, File file) {
        ClientConfiguration conf = new ClientConfiguration();
        conf.setSupportCname(false);
        // System.out.println("---------"+endpoint+"----------"+accessKeyId+"------"+accessKeySecret);
        OSSClient ossclient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
        try {
            System.out.println("Uploading a new object to OSS from a file\n");
            ossclient.putObject(new PutObjectRequest(bucketName, key, file));
        } catch (OSSException oe) {
            logger.info("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            logger.info("Error Message: " + oe.getErrorMessage());
            logger.info("Error Code:       " + oe.getErrorCode());
            logger.info("Request ID:      " + oe.getRequestId());
            logger.info("Host ID:           " + oe.getHostId());
        } catch (ClientException ce) {
            logger.info("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            logger.info("Error Message: " + ce.getMessage());
        } catch (Throwable e) {
            logger.error("上传OSS异常",e.getMessage());
        } finally {
            ossclient.shutdown();
        }
    }


    public void saveFilePic(String filepath,String serverPath, String fileName) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            byte[] bs = new byte[1024];
            int len;
            File tempFile = new File(serverPath);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            inputStream=new FileInputStream(filepath);
            outputStream = new FileOutputStream(tempFile + File.separator + fileName);
            while ((len = inputStream.read(bs)) != -1) {
                outputStream.write(bs, 0, len);
            }
        } catch (IOException e) {
            logger.error("保存文件异常",e.getMessage());
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                logger.error("保存文件关闭流异常",e.getMessage());
            }
        }
    }
}
